﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;

namespace NotebookS2
{
    public partial class M_USERPOLICY : RibbonForm
    {
        public M_USERPOLICY()
        {
            InitializeComponent();
        }


        public const int ObjectType = 13;
        public string ConnectionString { get; set; }
        public string ActiveUserID { get; set; }
        public DataTable LineItems { get; set; }


        public void ShowSearch()
        {
            FND_POLICY oForm = new FND_POLICY();

            oForm.ConnectionString = this.ConnectionString;
            oForm.ShowDialog();

            DataTable dtPolHeader = new DataTable();
            DataTable dtPolDetail = new DataTable();

            PolicyManager oManager = new PolicyManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            dtPolHeader = oManager.GetPolicy(oForm.SelectedGroupNum);
            dtPolDetail = oManager.GetPolicyLineItems(oForm.SelectedGroupNum);
            oManager.Close();

            this.LineItems = dtPolDetail;
            DataRow dRowHeader = dtPolHeader.Rows[0];
            txtGroupCode.Enabled = false;
            txtGroupCode.Text = dRowHeader["GroupCode"].ToString();
            txtPolicyName.Text = dRowHeader["PolicyGroupName"].ToString();

            txtDateCreated.Text = Convert.ToString(dRowHeader["DateCreated"]);
            txtCreatedBy.Text = dRowHeader["CreatedBy"].ToString();
            txtDateModified.Text = Convert.ToString(dRowHeader["DateModified"]);
            txtModifiedBy.Text = dRowHeader["ModifiedBy"].ToString();

            gridControl1.DataSource = this.LineItems;
            gridControl1.Refresh();

            btnAdd.Text = "Update";

        }

        DataTable InitializeTable()
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("ObjName", typeof(string));
            dt.Columns.Add("ObjType", typeof(int));
            dt.Columns.Add("AccessType", typeof(string));

            return dt;
        }

        public void BindObjectType()
        {
            ObjectTypeManager oManager = new ObjectTypeManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            DataTable dt = oManager.GetObjects();
            oManager.Close();


            foreach (DataRow dRow in dt.Rows)
            {
                DataRow _row = this.LineItems.NewRow();

                _row["ObjName"] = dRow["ObjName"].ToString();
                _row["ObjType"] = Convert.ToInt16(dRow["ObjType"]);
                 _row["AccessType"] = dRow["AccessType"];

                this.LineItems.Rows.Add(_row);
            }


            gridControl1.DataSource = this.LineItems;
            gridControl1.Refresh();

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void M_USERPOLICY_Load(object sender, EventArgs e)
        {
            this.LineItems = InitializeTable();
            BindObjectType();

            txtStatus.Text = "Active";
            txtDateCreated.Text = System.DateTime.Now.ToString();
            txtCreatedBy.Text = this.ActiveUserID;
            txtDateModified.Text = System.DateTime.Now.ToString();
            txtModifiedBy.Text = this.ActiveUserID;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            PolicyManager oManager = new PolicyManager();
            PolicyUnit oUnit = new PolicyUnit();

            if (btnAdd.Text == "Add")
            {
                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();

                oUnit.GroupCode = txtGroupCode.Text;
                oUnit.PolicyName = txtPolicyName.Text;
                oUnit.DocumentStatus = txtStatus.Text;
                oUnit.DateCreated = Convert.ToDateTime(txtDateCreated.Text);
                oUnit.CreatedBy = txtCreatedBy.Text;
                oUnit.DateModified = Convert.ToDateTime(txtDateModified.Text);
                oUnit.ModifiedBy = txtModifiedBy.Text;

                oUnit.LineItems = this.LineItems;
                oManager.AddPolicy(oUnit);

                MessageBox.Show("New policy group added successfully!!!", "User Policy", MessageBoxButtons.OK, MessageBoxIcon.Information);
                oManager.Close();
            }
            else
            {
                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();

                oUnit.GroupCode = txtGroupCode.Text;
                oUnit.PolicyName = txtPolicyName.Text;
                oUnit.DocumentStatus = txtStatus.Text;
                oUnit.DateCreated = Convert.ToDateTime(txtDateCreated.Text);
                oUnit.CreatedBy = txtCreatedBy.Text;
                oUnit.DateModified = Convert.ToDateTime(txtDateModified.Text);
                oUnit.ModifiedBy = txtModifiedBy.Text;

                oUnit.LineItems = this.LineItems;
                oManager.UpdatePolicy(oUnit);
                MessageBox.Show("Policy updated!!!", "User Policy", MessageBoxButtons.OK, MessageBoxIcon.Information);
                oManager.Close();
            }
        }

        private void M_USERPOLICY_Resize(object sender, EventArgs e)
        {
            gridControl1.Height = this.Height - 251;

            btnAdd.Top = this.Height - 89;
            btnCancel.Top = this.Height - 89;
        }
    }
}
